package com.ruoyi.standingbook.v2toilet.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.annotation.RepeatSubmit;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.PageQuery;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.core.validate.AddGroup;
import com.ruoyi.common.core.validate.EditGroup;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.standingbook.v2toilet.ItemEnum;
import com.ruoyi.standingbook.v2toilet.domain.bo.V2ToiletBillBo;
import com.ruoyi.standingbook.v2toilet.domain.vo.V2ToiletBillVo;
import com.ruoyi.standingbook.v2toilet.service.IToiletBillService;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import javax.validation.constraints.NotEmpty;
import javax.validation.constraints.NotNull;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

/**
 * V2市政公厕管养-异常工单
 *
 * @author cc
 * @date 2023-06-07
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/toiletv2/toiletBillV2")
public class V2ToiletBillController extends BaseController {

    private final IToiletBillService iToiletBillService;

    @SaCheckPermission("toiletv2:toiletV2:list")
    @GetMapping("/bills")
    public R<V2ToiletBillVo> list(V2ToiletBillBo bo) {

        return R.ok(iToiletBillService.flowData(bo));

    }

    @GetMapping("/items")
    public List<String> items() {
        return Arrays.stream(ItemEnum.values()).map(e -> e.attr).collect(Collectors.toList());
    }

    /**
     * 查询V2市政公厕管养-异常工单列表
     */
    @SaCheckPermission("toiletv2:toiletV2:list")
    @GetMapping("/list")
    public TableDataInfo<V2ToiletBillVo> list(V2ToiletBillBo bo, PageQuery pageQuery) {
        return iToiletBillService.queryPageList(bo, pageQuery);
    }


    /**
     * 导出V2市政公厕管养-异常工单列表
     */
    @SaCheckPermission("toiletv2:toiletV2:export")
    @Log(title = "V2市政公厕管养-异常工单", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(V2ToiletBillBo bo, HttpServletResponse response) {
        List<V2ToiletBillVo> list = iToiletBillService.queryList(bo);
        ExcelUtil.exportExcel(list, "V2市政公厕管养-异常工单", V2ToiletBillVo.class, response);
    }

    /**
     * 获取V2市政公厕管养-异常工单详细信息
     *
     * @param id 主键
     */
    @SaCheckPermission("toiletv2:toiletV2:query")
    @GetMapping("/{id}")
    public R<V2ToiletBillVo> getInfo(@NotNull(message = "主键不能为空")
                                     @PathVariable String id) {
        return R.ok(iToiletBillService.queryById(id));
    }

    /**
     * 新增V2市政公厕管养-异常工单
     */
    @SaCheckPermission("toiletv2:toiletV2:add")
    @Log(title = "V2市政公厕管养-异常工单", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody V2ToiletBillBo bo) {
        return toAjax(iToiletBillService.insertByBo(bo));
    }

    /**
     * 修改V2市政公厕管养-异常工单
     */
    @SaCheckPermission("toiletv2:toiletV2:edit")
    @Log(title = "V2市政公厕管养-异常工单", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody V2ToiletBillBo bo) {
        return toAjax(iToiletBillService.updateByBo(bo));
    }

    /**
     * 删除V2市政公厕管养-异常工单
     *
     * @param ids 主键串
     */
    @SaCheckPermission("toiletv2:toiletV2:remove")
    @Log(title = "V2市政公厕管养-异常工单", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空") @PathVariable String[] ids) {
        return toAjax(iToiletBillService.deleteWithValidByIds(Arrays.asList(ids), true));
    }
}
